﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using XsClient.Common;
using XsClient.Models;

namespace XsClient.Parsers
{
    /// <summary>
    /// BaseResolver
    /// 创建人：zhangyang
    /// 创建时间：2020/6/5 11:10:35
    /// </summary>
    public abstract class BaseResolver
    {
        protected XsSource _Source;
        /// <summary>
        /// 数据源
        /// </summary>
        public virtual XsSource Source { get { return _Source; } protected set { _Source = value; } }
        private string _Name;
        public virtual string Name { get { return _Name; } protected set { _Name = value; } }

        protected string seachUrl = "{0}";
        private HttpClientX httpClient = null;

        protected virtual string listRegex { get; }
        protected virtual string chaperRegex { get; }
        protected virtual string contextRegex { get; }

        public HttpClientX Client
        {
            get
            {
                if (httpClient == null)
                    httpClient = HttpClientX.Instance;
                return httpClient;
            }
        }


        public virtual List<BookInfo> Seach(string name)
        {
            List<BookInfo> list = new List<BookInfo>();
            try
            {
                string url = string.Format(seachUrl, HttpUtility.UrlEncode(name));
                string htmlStr = Client.GetHtml(ref url, Source.Encoding);
                NewLife.Log.XTrace.WriteLine(htmlStr);
                GetBaseUrl(url);
                return FindBooksByHtml(htmlStr);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return list;
        }
        public virtual List<BookInfo> FindBooks(string url)
        {
            string htmlStr = Client.GetHtml(ref url, Source.Encoding);
            GetBaseUrl(url);
            return FindBooksByHtml(htmlStr);
        }
        public abstract List<BookInfo> FindBooksByHtml(string html);

        public virtual List<XsUrlInfo> FindBookTypes()
        {
            return null;
        }
        /// <summary>
        /// 返回章节列表
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public abstract List<ChaperInfo> FindChapers(ref string url);
        /// <summary>
        /// 获取章节内容
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public abstract string GetContext(string url);

        protected virtual void InitSource(int key, XsSource source)
        {

            try
            {
                var xsConfig = XsConfig.Current;
                Source = xsConfig.FindById(key);
                if (Source == null)
                {
                    if (xsConfig.Sources == null)
                        xsConfig.Sources = new List<XsSource>();

                    Source = source;
                    xsConfig.Sources.Add(Source);
                    xsConfig.Save();
                }
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);
            }
        }

        protected virtual string GetBaseUrl(string url)
        {
            string baseUrl = null;
            //http://www.biquge.com/
            try
            {
                if (url.StartsWith(Source.BaseUrl))
                    return Source.BaseUrl;
                else
                {
                    int len = url.IndexOf('/', 9);
                    baseUrl = url.Substring(0, len) + "/";
                    Source.BaseUrl = baseUrl;

                    var config = XsConfig.Current;
                    for (int i = 0; i < config.Sources.Count; i++)
                    {
                        if (config.Sources[i].ID == Source.ID)
                        {
                            config.Sources[i].BaseUrl = baseUrl;
                            break;
                        }
                    }
                    config.Save();
                }
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);
            }


            return baseUrl;
        }
    }
}
